//
//  APPTextViewAlertViewController.swift
//  APP
//
//  Created by Rdxer on 2017/12/20.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import FastComponent
import UIKit

/// 自定义 customView
open class APPCustomViewAlertViewController: APPAlertViewController {
    
    var allowSideslip: Bool = false
    
    public var customViewH: CGFloat = 180
    
    public var customViewInsets: UIEdgeInsets = .init(top:  8, left:  8, bottom: -8, right: -8)

    public var customView: UIView

    public var needListenKeyboard = false

    deinit {
        if needListenKeyboard {
            removeListenKeyboard()
        }
    }

    public init(customView: UIView) {
        self.customView = customView
        super.init()
    }

    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override open func prepareSubViews() {
        if needListenKeyboard {
            prepareAddListenKeyboard()
        }

        view.addSubviews([
            backView,
            containerView.addSubviews([
                title_Label,
                customView,
                buttonsContainerView.addSubviews([
                    cancelButton,
                    confirmButton,
                ]),
            ]),
        ])
    }

    override open func layoutSubViews() {
        backView.snp.remakeConstraints { make in
            make.edges.equalToSuperview()
        }

        containerView.snp.remakeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(w)
        }

        title_Label.snp.remakeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(titleHeight)
        }

        buttonsContainerView.snp.remakeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(rowHeight)
        }

        customView.snp.remakeConstraints { make in
            make.top.equalTo(title_Label.snp.bottom).offset(customViewInsets.top)
            make.bottom.equalTo(buttonsContainerView.snp.top).offset(customViewInsets.bottom)
            make.left.equalToSuperview().offset(customViewInsets.left)
            make.right.equalToSuperview().offset(customViewInsets.right)
            make.height.equalTo(customViewH)
        }

        buttonsContainerView.xx_HorizontalTile(withSubviews: [cancelButton, confirmButton].filter({ btn in
            btn.isHidden == false
        }), insets: .zero)
    }
}

extension APPCustomViewAlertViewController {
    public func prepareAddListenKeyboard() {
        NotificationCenter.default.addObserver(self, selector: #selector(xx_UIKeyboardWillChangeFrame(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
    }

    public func removeListenKeyboard() {
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
    }

    /// 480
    @objc
    public  func xx_UIKeyboardWillChangeFrame(notification: NSNotification) {
        let info = notification.userInfo
        let kbRect = (info?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

        let offsetY = (480.from1080) + kbRect.origin.y - UIScreen.main.bounds.height

        let y = XXMathLibrary.absLimit(offsetY, min: -9999, max: 0)
        UIView.animate(withDuration: 0.3) {
            self.view.transform = CGAffineTransform(translationX: 0, y: y)
        }
    }
}
